<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        canvas{border: 1px solid #aaa;}
    </style>
</head>
<body>

<canvas width="600" height="400" class="myCanvas">
    当前的浏览器版本不支持，请升级浏览器
</canvas>
<script>
    var canvas = document.getElementsByClassName('myCanvas')[0];
    var ctx = canvas.getContext('2d');

    var linear = ctx.createLinearGradient(50,50,250,250);  //前两个是开始点，后两个是结束点
    linear.addColorStop(0,'red');
    linear.addColorStop(0.5,'yellow');
    linear.addColorStop(1,'purple');
    ctx.fillStyle = linear; //填充颜色
    ctx.fillRect(50,50,200,200);  //绘制矩形

    var Radia = ctx.createRadialGradient(400,150,20,400,150,50); //前三个是开始点，后三个是结束点
    Radia.addColorStop(0,'red');
    Radia.addColorStop(0.5,'yellow');
    Radia.addColorStop(1,'purple');
    ctx.fillStyle = Radia; //填充颜色
    ctx.fillRect(300,50,200,200);  //绘制矩形
</script>

</body>
</html>
